package com.cby.operation.anti.bean

import com.cby.operation.common.util.CbyLogUtils
import org.json.JSONArray
import org.json.JSONObject
import java.util.*

/**
 * Description:HykbFcmConfig
 *
 * @author 陈宝阳
 * @create 2019/12/31 16: 33
 */
class AntiConfig {
    /**
     * 下次心跳请求间隔时长，单位秒
     */
    var interval = 0
        private set

    /**
     * 本次会话对应的token
     */
    var token: String? = null
        private set

    /**
     * 还剩余的时间
     */
    var remainingTime = 0
        private set

    /**
     * 返回用户实名信息，1：成年；2：未成年；3：未实名
     */
    var idcardStatus = 0
        private set

    /**
     * 返回用户累计游戏时长，单位秒
     */
    var playTime = 0
        private set

    /**
     * 防沉迷UI体现的配置信息
     */
    private var popConfigs: MutableList<PopConfigBean> = mutableListOf()

    /**
     * 倒计时信息配置
     */
    private var countDownBeans: MutableList<PopConfigBean> = mutableListOf()

    /**
     * 结束游戏的操作
     */
    var offLineBean: PopConfigBean? = null
        private set

    fun parseData(dataJson: JSONObject) {
        interval = dataJson.optInt(KEY_INTERVAL)
        token = dataJson.optString(KEY_TOKEN)
        remainingTime = dataJson.optInt(KEY_REMAINING_TIME)
        idcardStatus = dataJson.optInt(KEY_IDCARD_STATUS)
        playTime = dataJson.optInt(KEY_PLAY_TIME)
        val popConfigJsonArray = dataJson.optJSONArray(KEY_POP_CONFIG)
        parsePopConfig(popConfigJsonArray)
        val countDownJsonArray = dataJson.optJSONArray(KEY_COUNT_DOWN)
        parseCountDown(countDownJsonArray)
        val offLineJson = dataJson.optJSONObject(KEY_OFF_LINE)
        parseOffLine(offLineJson)
    }

    private fun parsePopConfig(popConfigJsonArray: JSONArray?) {
        if (popConfigJsonArray != null) {
            val len = popConfigJsonArray.length()
            for (i in 0 until len) {
                val configJson = popConfigJsonArray.optJSONObject(i)
                val configBean = PopConfigBean()
                configBean.type = configJson.optInt(KEY_TYPE)
                configBean.onlyFirst = configJson.optInt(KEY_ONLY_FIRST)
                configBean.moment = configJson.optInt(KEY_MOMENT)
                configBean.title = configJson.optString(KEY_TITLE)
                configBean.content = configJson.optString(KEY_CONTENT)
                configBean.tips = parseTips(configJson.optJSONArray(KEY_TIPS))
                configBean.linkBean = parseLink(configJson.optJSONObject(KEY_LINK))
                configBean.buttonBeanList = parseButtons(configJson.optJSONArray(KEY_BUTTONS))
                configBean.finalBean = parseFinal(configJson.optJSONObject(KEY_FINAL))
                configBean.closeButtonBean = parseCloseButton(
                    configJson.optJSONObject(
                        KEY_CLOSE_BUTTONS
                    )
                )
                CbyLogUtils.i("m3839", configBean.toString())
                popConfigs.add(configBean)
            }
        }
    }

    private fun parseCountDown(popConfigJsonArray: JSONArray?) {
        countDownBeans = ArrayList()
        if (popConfigJsonArray != null) {
            val len = popConfigJsonArray.length()
            for (i in 0 until len) {
                val configJson = popConfigJsonArray.optJSONObject(i)
                val configBean = PopConfigBean()
                configBean.type = configJson.optInt(KEY_TYPE)
                configBean.onlyFirst = configJson.optInt(KEY_ONLY_FIRST)
                configBean.moment = configJson.optInt(KEY_MOMENT)
                configBean.title = configJson.optString(KEY_TITLE)
                configBean.content = configJson.optString(KEY_CONTENT)
                configBean.tips = parseTips(configJson.optJSONArray(KEY_TIPS))
                configBean.linkBean = parseLink(configJson.optJSONObject(KEY_LINK))
                configBean.buttonBeanList = parseButtons(configJson.optJSONArray(KEY_BUTTONS))
                configBean.finalBean = parseFinal(configJson.optJSONObject(KEY_FINAL))
                configBean.closeButtonBean = parseCloseButton(
                    configJson.optJSONObject(
                        KEY_CLOSE_BUTTONS
                    )
                )
                CbyLogUtils.i("m3839", configBean.toString())
                countDownBeans.add(configBean)
            }
        }
    }

    private fun parseTips(jsonArray: JSONArray?): List<String>? {
        if (jsonArray == null) {
            return null
        }
        val tips: MutableList<String> = ArrayList()
        val len = jsonArray.length()
        for (i in 0 until len) {
            tips.add(jsonArray.optString(i))
        }
        return tips
    }

    private fun parseLink(linkJson: JSONObject?): LinkBean? {
        if (linkJson == null) {
            return null
        }
        val name = linkJson.optString(KEY_NAME)
        val urlJson = linkJson.optJSONObject(KEY_URL_INFO)
        return if (urlJson != null) {
            val url = urlJson.optString(KEY_URL)
            val target = urlJson.optString(KEY_TARGET)
            LinkBean(name, url, target)
        } else {
            LinkBean(name, "", "")
        }
    }

    private fun parseButtons(jsonArray: JSONArray?): List<ButtonBean>? {
        if (jsonArray == null) {
            return null
        }
        val buttonBeans: MutableList<ButtonBean> = ArrayList()
        val len = jsonArray.length()
        for (i in 0 until len) {
            val buttonJson = jsonArray.optJSONObject(i)
            val name = buttonJson.optString(KEY_NAME)
            val func = buttonJson.optString(KEY_FUNC)
            val urlJson = buttonJson.optJSONObject(KEY_URL_INFO)
            if (urlJson != null) {
                val url = urlJson.optString(KEY_URL)
                val target = urlJson.optString(KEY_TARGET)
                buttonBeans.add(ButtonBean(name, func, url, target))
            } else {
                buttonBeans.add(ButtonBean(name, func))
            }
        }
        return buttonBeans
    }

    private fun parseCloseButton(closeJson: JSONObject?): CloseButtonBean? {
        if (closeJson == null) {
            return null
        }
        val show = closeJson.optBoolean(KEY_SHOW)
        val func = closeJson.optString(KEY_FUNC)
        return CloseButtonBean(show, func)
    }

    private fun parseFinal(finalJson: JSONObject?): FinalBean? {
        if (finalJson == null) {
            return null
        }
        val time = finalJson.optInt(KEY_TIME)
        val func = finalJson.optString(KEY_FUNC)
        val urlJson = finalJson.optJSONObject(KEY_URL_INFO)
        return if (urlJson != null) {
            val url = urlJson.optString(KEY_URL)
            val target = urlJson.optString(KEY_TARGET)
            FinalBean(time, func, url, target)
        } else {
            FinalBean(time, func)
        }
    }

    private fun parseOffLine(offLineJson: JSONObject?) {
        if (offLineJson == null) {
            return
        }
        offLineBean = PopConfigBean()
        offLineBean!!.type = offLineJson.optInt(KEY_TYPE)
        offLineBean!!.onlyFirst = offLineJson.optInt(KEY_ONLY_FIRST)
        offLineBean!!.title = offLineJson.optString(KEY_TITLE)
        offLineBean!!.content = offLineJson.optString(KEY_CONTENT)
        offLineBean!!.tips = parseTips(offLineJson.optJSONArray(KEY_TIPS))
        offLineBean!!.linkBean = parseLink(offLineJson.optJSONObject(KEY_LINK))
        offLineBean!!.buttonBeanList = parseButtons(offLineJson.optJSONArray(KEY_BUTTONS))
        offLineBean!!.finalBean = parseFinal(offLineJson.optJSONObject(KEY_FINAL))
        offLineBean!!.closeButtonBean =
            parseCloseButton(offLineJson.optJSONObject(KEY_CLOSE_BUTTONS))
    }

    fun resetToken() {
        token = null
    }

    fun getPopConfigs(): List<PopConfigBean>? {
        return popConfigs
    }

    fun getCountDownBeans(): List<PopConfigBean>? {
        //降序
        countDownBeans.sortWith(Comparator { o1, o2 -> o2.moment - o1.moment })
        return countDownBeans
    }

    companion object {
        private const val KEY_INTERVAL = "interval"
        private const val KEY_TOKEN = "token"
        private const val KEY_REMAINING_TIME = "time_remaining"
        private const val KEY_IDCARD_STATUS = "idcard_status"
        private const val KEY_PLAY_TIME = "playtime"
        private const val KEY_POP_CONFIG = "pop_config"
        private const val KEY_TYPE = "type"
        private const val KEY_ONLY_FIRST = "only_first"
        private const val KEY_TITLE = "title"
        private const val KEY_CONTENT = "content"
        private const val KEY_TIPS = "tips"
        private const val KEY_LINK = "link"
        private const val KEY_BUTTONS = "buttons"
        private const val KEY_FINAL = "final"
        private const val KEY_CLOSE_BUTTONS = "close_button"
        private const val KEY_NAME = "name"
        private const val KEY_FUNC = "func"
        private const val KEY_URL_INFO = "url_info"
        private const val KEY_URL = "url"
        private const val KEY_TARGET = "target"
        private const val KEY_TIME = "time"
        private const val KEY_SHOW = "show"
        private const val KEY_COUNT_DOWN = "countdown"
        private const val KEY_MOMENT = "moment"
        private const val KEY_OFF_LINE = "offline"
    }
}